﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Chilis.Marketing.Common
{
    public static class ControlHelper
    {
        /// <summary>
        /// Registers the client-side script file.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="fileName">Name of the file.</param>
        /// <param name="isLibraryScript">if set to <c>true</c> 
        /// the specified file is located in the Self-Service script library.</param>
        public static void RegisterClientScriptFile(Page page, string fileName, bool isLibraryScript)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                throw new ArgumentNullException("fileName");
            }

            if (page == null)
            {
                throw new ArgumentNullException("page");
            }

            string scriptKey = fileName.ToUpperInvariant();
            if (!page.ClientScript.IsClientScriptIncludeRegistered(scriptKey))
            {
                string clientPath = (isLibraryScript) ?
                    StringHelper.FormatForCode("~/Scripts/{0}",
                                                fileName) : fileName;

                page.ClientScript.RegisterClientScriptInclude(scriptKey, page.ResolveClientUrl(clientPath));
            }
        }
    }
}
